package com.yogee.topseller.activity.subscription;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yogee.core.base.HttpActivity;
import com.yogee.core.http.listener.HttpOnNextListener;
import com.yogee.core.http.subscribers.BaseSubscriber;
import com.yogee.core.utils.ToastUtils;
import com.yogee.topseller.R;
import com.yogee.topseller.activity.subscription.bean.ProductAppointBean;
import com.yogee.topseller.http.HttpManager;
import com.yogee.topseller.utils.AppUtil;
import com.yogee.topseller.view.ClearEditText;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SubscriptionProductActivity extends HttpActivity {

    @BindView(R.id.confirm)
    TextView confirm;
    @BindView(R.id.title_white_left_back)
    LinearLayout titleWhiteLeftBack;
    @BindView(R.id.title_white_title)
    TextView titleWhiteTitle;
    @BindView(R.id.cet_subproduct_name)
    ClearEditText cetSubproductName;
    @BindView(R.id.cet_subperson_name)
    ClearEditText cetSubpersonName;
    @BindView(R.id.cet_subperson_phone)
    ClearEditText cetSubpersonPhone;
    @BindView(R.id.cet_submoney)
    ClearEditText cetSubmoney;
    @BindView(R.id.iv_money_flag)
    ImageView ivMoneyFlag;
    @BindView(R.id.cet_submoney_upper)
    TextView cetSubmoneyUpper;
    @BindView(R.id.title_white_right_txt)
    TextView title_white_right_txt;



    //是否先配送合同再打款标识
    int beforeFlag = 0;
    //认购产品id
    String proId = "";
    private String isFast = "0";
    private String startAmount;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_subscription_product;
    }

    @Override
    protected void initView() {
        titleWhiteTitle.setText("认购");
        title_white_right_txt.setVisibility(View.VISIBLE);
        title_white_right_txt.setText("确认");
        if (null != getIntent()) {
            proId = getIntent().getStringExtra("id");
            cetSubproductName.setText(getIntent().getStringExtra("name"));
            startAmount = getIntent().getStringExtra("startAmount");
            isFast = getIntent().getStringExtra("isFast");
        }
        cetSubmoney.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!"".equals(cetSubmoney.getText().toString())){
                    cetSubmoneyUpper.setText(AppUtil.digitUppercase(Double.parseDouble(cetSubmoney.getText().toString()))+"万元");
                }else{
                    cetSubmoneyUpper.setText("零万元");
                }
            }
        });
    }

    @OnClick({R.id.title_white_right_txt, R.id.title_white_left_back, R.id.iv_money_flag})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_white_left_back:
                finish();
                break;
            case R.id.title_white_right_txt:
                if (checkView()) {
                    confirmSub();
                }
                break;
            case R.id.iv_money_flag:
                if (0 == beforeFlag) {
                    ivMoneyFlag.setImageDrawable(getResources().getDrawable(R.drawable.select1));
                    beforeFlag = 1;
                } else {
                    ivMoneyFlag.setImageDrawable(getResources().getDrawable(R.drawable.unselect1));
                    beforeFlag = 0;
                }
                break;
        }
    }

    private boolean checkView() {
        if (TextUtils.isEmpty(cetSubpersonName.getText().toString())) {
            ToastUtils.showToast(this, "请填写姓名");
            return false;
        } else if (TextUtils.isEmpty(cetSubpersonPhone.getText().toString())) {
            ToastUtils.showToast(this, "请填写手机号码");
            return false;
        } else if (!AppUtil.validatePhone(cetSubpersonPhone.getText().toString())) {
            ToastUtils.showToast(this, "请填写11位手机号码");
            return false;
        } else if (TextUtils.isEmpty(cetSubmoney.getText().toString())) {
            ToastUtils.showToast(this, "请填写认购金额");
            return false;
        } else if (Integer.valueOf(startAmount) > Integer.valueOf(cetSubmoney.getText().toString())) {
            ToastUtils.showToast(this, "认购金额必须大于起投金额");
            return false;
        }
        return true;
    }

    private void confirmSub() {
        HttpManager.getInstance().productSub(proId, AppUtil.getUserId(this)
                , cetSubpersonName.getText().toString()
                , cetSubpersonPhone.getText().toString()
                , cetSubmoney.getText().toString().replace("万", "")
                , String.valueOf(beforeFlag)
                , isFast
        ).compose(this.bindRecycler()).subscribe(new BaseSubscriber(new HttpOnNextListener<ProductAppointBean>() {
            @Override
            public void onNext(ProductAppointBean o) {
                if ("1".equals(o.getResult())) {
                    startActivity(new Intent(SubscriptionProductActivity.this, ContractDistributionActivity.class).putExtra("type", "0").putExtra("proId", o.getOrderId()));
                    finish();
                }
            }
        }, this));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
